package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Loser
 * @Date: Created in 2020-10-15 11:41
 * @Description: 权限管理表现层
 * @Path: com.itheima.controller.PermissionController
 * @Modified By:
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 添加权限
     * @param permission
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Permission permission) {
        try{
            permissionService.addPsermisson(permission);
            return new Result(true,"添加权限成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加权限失败",null);
        }
    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try{
            PageResult pageResult = permissionService.findPage(queryPageBean);
            return new Result(true,"查询权限成功",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询权限失败",null);
        }
    }

    /**
     * 修改权限
     * @param permission
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Permission permission){
        try{
            permissionService.edit(permission);
            return new Result(true,"修改权限成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改权限失败",null);
        }
    }

    /**
     * 根据id查询数据，数据回显
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try{
            Permission permission = permissionService.findById(id);
            return new Result(true,"权限回显成功",permission);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"权限回显失败",null);
        }
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try{
            permissionService.delete(id);
            return new Result(true,"删除成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除失败！",null);
        }
    }
}
